package xyz.esion.povertyalleviation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.esion.povertyalleviation.entry.R;
import xyz.esion.povertyalleviation.entry.VVillageAll;
import xyz.esion.povertyalleviation.service.VVillageAllService;

/**
 * @author Esion
 * @since 2021/5/5
 */
@RestController
@RequestMapping("api/search")
public class SearchController {

    private final VVillageAllService villageService;

    @Autowired
    public SearchController(VVillageAllService villageService) {
        this.villageService = villageService;
    }

    @GetMapping("base/{type}")
    public R base(@PathVariable("type") Integer type, @RequestParam(value = "value", defaultValue = "", required = false) String value){
        if("".equals(value)){
            return R.success();
        }
        QueryWrapper<VVillageAll> wrapper = new QueryWrapper<>();
        switch (type){
            case 1:
                // 根据名称
                wrapper.like("info_name", value);
                break;
            case 2:
                // 根据类型
                wrapper.eq("type_id", value);
                break;
            case 3:
                wrapper.eq("county_id", value);
                break;
            case 4:
                wrapper.eq("id", value);
                break;
            default:
                return R.fail();
        }
        return R.success().data("items", villageService.list(wrapper));
    }

}
